package com.gzzhsdcm.czh.myxw.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import com.gzzhsdcm.czh.myxw.R;
import com.gzzhsdcm.czh.myxw.frgment.ShoyeFragment;
import com.gzzhsdcm.czh.myxw.view.ModifyTabLayout;
import com.gzzhsdcm.czh.myxw.view.WrapContentHeightViewPager;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.text.Format;
import java.util.ArrayList;
import java.util.List;

import static com.gzzhsdcm.czh.myxw.httpurl.HttpUrl.LANMU;

/**
 * @fileName HomeActivity
 * @date on 2018-12-21 15:13
 * @company 贵州智合时代传媒有限公司
 * @developers czh
 * @email 1632365610@qq.com
 */
@ContentView(R.layout.activity_home)
public class HomeActivity extends AppCompatActivity {
    @ViewInject(R.id.mod_youji_title)
    private ModifyTabLayout modifyTabLayout;
    @ViewInject(R.id.vp_youji_fragemnt)
    private WrapContentHeightViewPager viewPager;
    private List<String> listT = new ArrayList<>();
    private List<Fragment> list = new ArrayList<>();
    private Myadapterer adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initView();
        hpptQinqiu();
    }

    private void hpptQinqiu() {
        OkGo.<String>post(LANMU)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject jsonObject  = new JSONObject(response.body());
                            JSONArray jsonArray = jsonObject.getJSONArray("data");
                            for (int i = 0;i<jsonArray.length();i++){
                                JSONObject object = jsonArray.getJSONObject(i);
                                listT.add(object.optString("typename"));
                                list.add(new ShoyeFragment().setIndex(object.optString("type")));
                            }
                            viewPager.setAdapter(adapter);
                            viewPager.setOffscreenPageLimit(adapter.getCount());
                            modifyTabLayout.setupWithViewPager(viewPager);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void initView() {
        modifyTabLayout.setViewHeight(dp2px(35));
        modifyTabLayout.setBottomLineWidth(dp2px(10));
        modifyTabLayout.setBottomLineHeight(dp2px(3));
        modifyTabLayout.setBottomLineHeightBgResId(R.color.color_14805E);
        modifyTabLayout.setItemInnerPaddingLeft(dp2px(6));
        modifyTabLayout.setItemInnerPaddingRight(dp2px(6));
        modifyTabLayout.setmTextColorSelect(ContextCompat.getColor(this,R.color.color_14805E));
        modifyTabLayout.setmTextColorUnSelect(ContextCompat.getColor(this,R.color.color_666666));
        modifyTabLayout.setTextSize(16);
        adapter = new Myadapterer(getSupportFragmentManager());

    }

    /**
     * dp转换成px
     */
    public int dp2px( float dpValue){
        float scale=getResources().getDisplayMetrics().density;
        return (int)(dpValue*scale+0.5f);
    }
    //    栏目adapter
    class Myadapterer extends FragmentPagerAdapter {


        public Myadapterer(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
        //重写这个方法，将设置每个Tab的标题
        @Override
        public CharSequence getPageTitle(int position) {
            return listT.get(position);
        }
    }


}
